package com.example.NearByLocation;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.baidu.mapapi.map.*;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.overlayutil.PoiOverlay;
import com.baidu.mapapi.search.core.CityInfo;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2014/11/27.
 */
public class PoISearchManager {
    private PoiSearch poiSearch;
    private int pageNum = 0;
    public List<String> list = new ArrayList<String>();
    public void makePoi(final Context context, final BaiduMap mBaiduMap, LatLng currLal, String keyWord, int radius) {
        poiSearch = PoiSearch.newInstance();

        OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                if (poiResult == null || poiResult.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
                    Toast.makeText(context, "未搜索到结果", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (poiResult.error == SearchResult.ERRORNO.NO_ERROR) {


                    mBaiduMap.clear();
                    PoiOverlay overlay = new MyPoiOverlay(mBaiduMap);
                    mBaiduMap.setOnMarkerClickListener(overlay);
                    overlay.setData(poiResult);
                    overlay.addToMap();
                    overlay.zoomToSpan();

                    return;
                }
                if (poiResult.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
                    String info = "在";
                    for (CityInfo cityInfo : poiResult.getSuggestCityList()) {
                        info += cityInfo.city;
                        info += ",";
                    }
                    info = "找到结果";
                    Toast.makeText(context, info, Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

            }
        };
        poiSearch.setOnGetPoiSearchResultListener(poiListener);
        poiSearch.searchNearby(new PoiNearbySearchOption().location(currLal).keyword(keyWord).radius(radius).pageNum(pageNum++));
    }


    private class MyPoiOverlay extends PoiOverlay {
        public MyPoiOverlay(BaiduMap mBaiduMap) {
            super(mBaiduMap);
        }

        @Override
        public boolean onPoiClick(int i) {
            super.onPoiClick(i);
            PoiInfo poi = getPoiResult().getAllPoi().get(i);
            // if (poi.hasCaterDetails) {

            poiSearch.searchPoiDetail((new PoiDetailSearchOption())
                    .poiUid(poi.uid));
            // }

            return true;
        }
    }

    public List<String> getList(final Context context,LatLng currLal, String keyWord, int radius){


        poiSearch = PoiSearch.newInstance();

        OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
            final List<String> list = new ArrayList<String>();
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                if (poiResult == null || poiResult.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
                    Toast.makeText(context, "未搜索到结果", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (poiResult.error == SearchResult.ERRORNO.NO_ERROR) {

                    list.add("牛肉面");
                    list.add("黄焖鸡");
                    Log.d("list", list.size() + "");
                    PoISearchManager p = new PoISearchManager();

//                    for (int i = 0; i <poiResult.getAllPoi().size() ; i++) {
//                        list.add(poiResult.getAllPoi().get(i));
//                    }
                }
            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
            }
        };
        poiSearch.setOnGetPoiSearchResultListener(poiListener);
        poiSearch.searchNearby(new PoiNearbySearchOption().location(currLal).keyword(keyWord).radius(radius).pageNum(pageNum++));
        Log.d("list111", list.size() + "");
        return list;

    }

    public void makeMark(LatLng currentLal,BaiduMap mBaiduMap){
        BitmapDescriptor bd = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);
        OverlayOptions oob = new MarkerOptions().position(currentLal).icon(bd).zIndex(7);
        mBaiduMap.addOverlay(oob);
    }
}